package com.hyt.it.ogt.kq.main.controller.admission;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.core.util.FastJsonUtil;
import com.hyt.exception.annotation.ThrowsAdvice;
import com.hyt.it.ogt.kq.common.gov.config.swagger.ApiVersionConstant;
import com.hyt.it.ogt.kq.common.gov.constant.ResponseCode;
import com.hyt.it.ogt.kq.main.controller.base.BaseController;
import com.hyt.it.ogt.kq.common.config.exception.KqException;
import com.hyt.it.ogt.kq.service.model.entity.admission.AdmissionTicketInfo;
import com.hyt.it.ogt.kq.service.admission.IAdmissionTicketInfoService;
import com.hyt.it.ogt.kq.service.admission.IExamineeInfoService;
import com.hyt.it.ogt.kq.service.admission.IExamineeOperateService;
import com.hyt.it.ogt.kq.service.gov.model.param.admission.ExamineeTicketViewPageParam;
import com.hyt.it.ogt.kq.service.gov.model.vo.admission.AdmissionQueryVO;
import com.hyt.it.ogt.kq.service.gov.model.vo.admission.ExamineeInfoVO;
import com.hyt.log.annotation.Log;
import com.hyt.loginfo.model.User;
import com.hyt.model.PageParam;
import com.hyt.progress.creator.anno.ProgressAnno;
import com.hyt.progress.service.ProgressManager;
import com.hyt.swagger.ApiVersion;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 准考证查询 前端控制器
 * </p>
 *
 * @since 2023-1-13
 */
@RestController
@Api(tags = "235.准考证查询接口", value = "准考证查询接口")
@ApiSort(value = 235)
@RequestMapping("/kw/gov/admission/ticket/")
@Slf4j
public class AdmissionQueryController extends BaseController {

    @Resource
    private IExamineeInfoService examineeInfoService;

    @Resource
    private IAdmissionTicketInfoService iAdmissionTicketInfoService;

    @Resource
    private IExamineeOperateService iExamineeOperateService;

    @ApiOperation(
            value = "235.1 根据考生姓名和准考证号，身份证号查询准考证",
            notes = "30000:成功;43350101:根据考生姓名和转考证号，身份证号查询准考证异常",
            response = AdmissionQueryVO.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "admissionTicketInfoId", value = "准考证信息id", dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "queryName", value = "查询用户姓名", dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "queryNumber", value = "查询用户号码", dataType = "string", paramType = "query", required = true)
    })
    @ApiOperationSort(value = 1)
    @ApiVersion(group = ApiVersionConstant.V2_4_6)
    @Log(module = "准考证查询", business = "考生查询准考证")
    @ThrowsAdvice(code = 43350101, print = "查询准考证异常")
    @GetMapping("/queryByNameAndIdCardNo")
    public ApiResponse<AdmissionQueryVO> queryByNameAndIdCardNo(
            @RequestParam(required = true, value = "admissionTicketInfoId") String admissionTicketInfoId,
            @RequestParam(required = true, value = "queryName") String queryName,
            @RequestParam(required = true, value = "queryNumber") String queryNumber) {

		log.info("## queryByNameAndIdCardNo params:{},{},{}", admissionTicketInfoId, queryName, queryNumber);
		try {
			return ApiResponse.<AdmissionQueryVO> builder().code(ResponseCode.SUCCESS.getCode())
					.data(examineeInfoService.queryByNameAndIdCardNo(admissionTicketInfoId, queryName, queryNumber))
					.build();
		} catch (KqException e) {
			log.error("## queryByNameAndIdCardNo查询考生准考证信息失败", e);
			return ApiResponse.<AdmissionQueryVO> builder().code(e.getErrCode()).data(null).build();
		} catch (Exception e) {
			log.error("## queryByNameAndIdCardNo查询考生准考证信息失败", e);
			return ApiResponse.<AdmissionQueryVO> builder().code(43350101).data(null).build();
		}
    }


    @ApiOperation(
            value = "35.2 根据考生姓名和转考证号，身份证号下载准考证",
            notes = "30000:成功;43350201:根据考生姓名和转考证号，身份证号下载准考证异常",
            response = String.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "admissionTicketInfoId", value = "准考证信息id", dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "examineeId", value = "考生examineeId", dataType = "string", paramType = "query", required = true)
    })
    @ApiOperationSort(value = 2)
    @ApiVersion(group = ApiVersionConstant.V2_4_6)
    @Log(module = "准考证查询", business = "考生下载准考证")
    @ThrowsAdvice(code = 43350201, print = "查询准考证异常")
    @GetMapping("/downloadByNameAndIdCardNo")
    public ApiResponse<String> downloadByNameAndIdCardNo(
            @RequestParam(required = true, value = "admissionTicketInfoId") String admissionTicketInfoId,
            @RequestParam(required = true, value = "examineeId") String examineeId,
            HttpServletResponse response) {
        log.info("## downloadByNameAndIdCardNo params:{},{}", admissionTicketInfoId, examineeId);
        return ApiResponse.<String>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .data(examineeInfoService.downloadByNameAndIdCardNo(response, admissionTicketInfoId, examineeId))
                .build();
    }


    @ApiOperation(
            value = "35.21 获取机构下准考证列表",
            notes = "30000:成功;43352101:获取机构下准考证列表异常, 43352102:获取不到当前登录人信息",
            response = AdmissionTicketInfo.class
    )
    @ApiOperationSort(value = 21)
    @ApiVersion(group = ApiVersionConstant.V2_4_6)
    @Log(module = "准考证查询", business = "获取机构下准考证列表")
    @ThrowsAdvice(code = 43352101, print = "查询准考证列表异常")
    @GetMapping("/queryTicketList")
    public ApiResponse<List<AdmissionTicketInfo>> queryTicketList() {
        User currUser = getUser();
        if (null == currUser || StringUtils.isEmpty(currUser.getId())) {
            return ApiResponse.<List<AdmissionTicketInfo>>builder()
                    .code(43352102)
                    .data(null)
                    .build();
        }
        return ApiResponse.<List<AdmissionTicketInfo>>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .data(iAdmissionTicketInfoService.queryTicketListByOfficeId(currUser.getOfficeid(), null))
                .build();
    }

    @ApiOperation(
            value = "35.31 获取机构下准考证查看列表",
            notes = "30000:成功;43353101:获取机构下准考证查看列表异常, 43353102:获取不到当前登录人信息",
            response = ExamineeInfoVO.class
    )
    @ApiOperationSort(value = 31)
    @ApiVersion(group = ApiVersionConstant.V2_4_6)
    @Log(module = "准考证查询", business = "获取机构下准考证查看列表")
    @ThrowsAdvice(code = 43353101, print = "获取机构下准考证查看列表异常")
    @PostMapping("/queryTicketViewList")
    public ApiResponse<Page<ExamineeInfoVO>> queryTicketViewList(@RequestBody ExamineeTicketViewPageParam examineeTicketViewPageParam) {
        User currUser = getUser();
        Page<ExamineeInfoVO> page = new PageParam<>();
        page.setCurrent(examineeTicketViewPageParam.getCurrent());
        page.setSize(examineeTicketViewPageParam.getSize());
        if (null == currUser || StringUtils.isEmpty(currUser.getId())) {
            return ApiResponse.<Page<ExamineeInfoVO>>builder()
                    .code(43353102)
                    .data(null)
                    .build();
        }
        return ApiResponse.<Page<ExamineeInfoVO>>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .data(examineeInfoService.queryTicketViewList(currUser.getOfficeid(), page, examineeTicketViewPageParam))
                .build();
    }

    @ApiOperation(
            value = "35.32 准考证查询下载记录导出",
            notes = "30000:成功;43353201:导出异常, 43353202:获取不到当前登录人信息",
            response = ExamineeInfoVO.class
    )
    @ApiOperationSort(value = 32)
    @ApiVersion(group = ApiVersionConstant.V2_4_6)
    @Log(module = "准考证查询", business = "准考证查询下载记录导出")
    @ThrowsAdvice(code = 43353201, print = "准考证查询下载记录导出异常")
    @PostMapping("/viewListExport")
    @ProgressAnno(total = 100, timeoutSeconds = 6000, errorCode = 43353201, canConcurrent = false)
    public void viewListExport( @RequestBody ExamineeTicketViewPageParam examineeTicketViewPageParam) {
        User currUser = getUser();
        if (null == currUser || StringUtils.isEmpty(currUser.getId())) {
            ProgressManager.updateProgressFailMessage(43353202, "导入失败，获取不到登录人信息");
            return;
        }
        log.info("viewListExport params:{},{}", currUser.getId(), FastJsonUtil.getBeanToJson(examineeTicketViewPageParam));
        try {
            iExamineeOperateService.exportExamineeOperateList(examineeTicketViewPageParam);
        } catch (Exception e) {
            log.error("viewListExport error", e);
            ProgressManager.updateProgressFailMessage(43353202, "导出失败");
        }
    }
}
